#include "cobra11syndicate.hlsli"
#include "..\\standard.hlsli"

PS_Input_C11 vs_main(VS_INPUT_C11 vsIn)
{
	PS_Input_C11 psIn = (PS_Input_C11)0;

	float4 c0 = float4(2, -1, -1.44269502, -0); // def c0, 2, -1, -1.44269502, -0
		
		float3 normal = float3(vsIn.Normal * c0.x + c0.y); // mad r0.xyz, v1, c0.x, c0.y
		float normalW = normal.x * normal.x + normal.y * normal.y + normal.z * normal.z; // dp3 r0.w, r0, r0
	normalW = 1.0f / sqrt(normalW);	// rsq r0.w, r0.w
	psIn.normal.xyz = normalW  *  normal.xyz; // mul o3.xyz, r0.w, r0

	float4 position = vsIn.Position + WorldOffset;  // add r0, c33, v0
		position.w = 1.0f;

		psIn.position = float4(mul(position, worldViewProj));
	//	psIn.position.x = position.x * worldViewProj[0].x + position.y * worldViewProj[0].y + position.z * worldViewProj[0].z + position.w * worldViewProj[0].w;	// dp4 o0.x, r0, c20
	//psIn.position.y = position.x * worldViewProj[1].x + position.y * worldViewProj[1].y + position.z * worldViewProj[1].z + position.w * worldViewProj[1].w;	// dp4 o0.y, r0, c21
	//psIn.position.z = position.x * worldViewProj[2].x + position.y * worldViewProj[2].y + position.z * worldViewProj[2].z + position.w * worldViewProj[2].w;	// dp4 o0.z, r0, c22
	//psIn.position.w = position.x * worldViewProj[3].x + position.y * worldViewProj[3].y + position.z * worldViewProj[3].z + position.w * worldViewProj[3].w;	// dp4 o0.w, r0, c23


	psIn.texCoord7.w = position.x * view[2].x + position.y * view[2].y + position.z * view[2].z;// dp4 o7.w, r0, c11
	float3 distanceToCamera = position.xyz + -g_CameraPosition;  // add r1.xyz, r0, -c32
		distanceToCamera = distanceToCamera * RangeFog.w;	// mul r1.xyz, r1, c31.w

	position.w = distanceToCamera.x * distanceToCamera.x + distanceToCamera.y * distanceToCamera.y + distanceToCamera.z * distanceToCamera.z;// dp3 r0.w, r1, r1
	position.w = position.w  * c0.z; // mul r0.w, r0.w, c0.z

	psIn.texCoord4.w = 2 * position.w; // exp o4.w, r0.w

	float4 color = float4(vsIn.kblend.xyz * vsIn.kblend.xyz * AmbLevel, vsIn.kblend.w); // mul r1.xyz, v5, v5 
		//color.xyz = color *  AmbLevel; // mul r1.xyz, r1, c30 // wurdee oben integriert.
		// color.w = vsIn.kblend.w; // mov r1.w, v5.w // wurdee oben integriert.
		psIn.texCoord2 = color + TerrainAmbient; // add o2, r1, c48	


	// Mat1
	float3 texcoord1 = float3(vsIn.Tex0.x * -c0.y + c0.w, vsIn.Tex0.y * -c0.y + c0.w, vsIn.Tex0.z * -c0.w + c0.y);  // mad r1.xyz, v2.xyxw, -c0.yyww, -c0.wwyw
		float2 textcoordinateMat1 = float2(dot(texcoord1, TexMat1[0]),	// dp3 r2.x, r1, c34
		dot(texcoord1, TexMat1[1]));  // dp3 r2.y, r1, c35
	psIn.texCoord5.zw = mad(textcoordinateMat1.xyxy, TexUVMod1.z, TexUVMod1.xyxy); // mad o5.zw, r2.xyxy, c38.z, c38.xyxy
	psIn.texCoord5.xy = textcoordinateMat1;// mov o5.xy, r2

	// Mat2
	float3 texcoord2 = mad(vsIn.Tex1.xyxw, -c0.yyww, -c0.wwyw); // mad r1.xyz, v3.xyxw, -c0.yyww, -c0.wwyw
		float2  textcoordinateMat2 = float2(dot(texcoord2, TexMat2[0]),	// dp3 r2.x, r1, c36
		dot(texcoord2, TexMat2[1])); // dp3 r2.y, r1, c37
	psIn.texCoord6.zw = mad(textcoordinateMat2.xyxy, TexUVMod2.z, TexUVMod2.xyxy); // mad o6.zw, r2.xyxy, c39.z, c39.xyxy
	psIn.texCoord6.xy = textcoordinateMat2; // mov o6.xy, r2

	psIn.texCoord4.xyz = -position + g_CameraPosition; // add o4.xyz, -r0, c32

	float4 pos = mad(position.xyzx, -c0.yyyw, -c0.wwwy); // mad r0, r0.xyzx, -c0.yyyw, -c0.wwwy
		psIn.texCoord8.x = dot(pos, ShadowMtx[0]); //dp4 o8.x, r0, c24]
	psIn.texCoord8.y = dot(pos, ShadowMtx[1]); // dp4 o8.y, r0, c25
	psIn.texCoord8.z = dot(pos, ShadowMtx[2]); // dp4 o8.z, r0, c26

	psIn.texCoord7.xyz = pos; // mov o7.xyz, r0
	psIn.color0 = vsIn.kColor; // mov o1, v4


	return psIn;
}
